using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrainManager : LogicModuleBase, ITrainManager
{
    List<Train.TrainType> defaultTrains = new List<Train.TrainType>();
    List<TrainBody> trains = new List<TrainBody>();

    GameObject CurrentTrain;

    public List<TrainBody> Trains => trains;

    public Train Train => CurrentTrain.GetComponent<Train>();

    //Dictionary<Train.TrainType, int> AllTrain = new Dictionary<Train.TrainType, int>();

    public override void Init()
    {
        base.Init();
        //AllTrain.Add(Train.TrainType.Head, 0);
        //AllTrain.Add(Train.TrainType.Body, 0);
        defaultTrains.Add(Train.TrainType.Head);
        defaultTrains.Add(Train.TrainType.Body);
        defaultTrains.Add(Train.TrainType.Body);
        defaultTrains.Add(Train.TrainType.Body);
    }

    public GameObject InstantiateTrain(bool isRight)
    {
        if (CurrentTrain != null)
        {
            GameObject.Destroy(CurrentTrain);
        }
        GameObject train = new GameObject("Train");
        train.AddComponent<Train>().isRight = isRight;
        for(int i = 0; i < defaultTrains.Count; i++)
        {
            switch (defaultTrains[i])
            {
                case Train.TrainType.Head:
                    GameObject head = ResourceManager.Instance.Instantiate("Prefabs/Train/Train_Head");
                    head.transform.parent = train.transform;
                    head.transform.localPosition = new Vector3(i * 4, 0, 0);
                    trains.Add(head.GetComponent<TrainBody>());
                    break;
                case Train.TrainType.Body:
                    GameObject body = ResourceManager.Instance.Instantiate("Prefabs/Train/Train_Body");
                    body.transform.parent = train.transform;
                    body.transform.localPosition = new Vector3(i * 4, 0, 0);
                    trains.Add(body.GetComponent<TrainBody>());
                    break;
                case Train.TrainType.Other:
                    break;
            }
        }
        if (isRight)
        {
            train.transform.eulerAngles = new Vector3(0, 0, 180);
        }
        CurrentTrain = train;
        UIManager.Instance.GetUI("MainUI").GetComponent<MainUI>().CreatHPBarOfTrain();
        AudioControl.Instance.PlaySound("train-stop", 2);
        return train;
    }

    public void Damage(TrainBody train, float damage)
    {
        train.health -= damage;
        if (train.health <= 0)
        {
            train.health = 0;
            train.transform.localEulerAngles = new Vector3(0, 0, 30);
            train.isFlip = true;
            train.counter = 0.75f * train.counter;
        }

        float r = Random.value;
        int n = (int)(r / 0.25f + 1);
        float v = Random.value / 100f;
        AudioControl.Instance.PlaySound("metal-hit-deep-" + n, v);
    }

    public void Fixed(TrainBody train)
    {
        train.transform.localEulerAngles = new Vector3(0, 0, 0);
        train.health = train.maxHealth / 2;
        train.isFlip = false;
    }
}